<template>
  <section>
    <!--数据字典高级查询工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="请输入数据字典名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="getQuerySdts">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="sdts" highlight-current-row @row-dblclick="sdtDailTable" v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="300">
      </el-table-column>
      <el-table-column prop="sn" label="编号" width="300" sortable>
      </el-table-column>
      <el-table-column prop="name" label="数据字典名" width="300" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--数据字典批量删除工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="sdtPage.currentPage"
          :page-sizes="sdtPage.pageSizes"
          :page-size="sdtPage.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="sdtPage.total"
          style="float:right;">
      </el-pagination>
    </el-col>

    <!--保存界面-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="字典编号" prop="sn">
          <el-input v-model="saveForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典名" prop="name">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>
    <!--字典明细保存界面-->
    <el-dialog :title="detailTitle" :visible.sync="saveDetailFormVisible" :close-on-click-modal="false">
      <el-form :model="saveDetailForm" label-width="80px" :rules="saveDetailFormRules" ref="saveDetailForm">
        <el-form-item label="明细名" prop="name">
          <el-input v-model="saveDetailForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveDetailFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addDetailSubmit" :loading="saveDetailLoading">提交</el-button>
      </div>
    </el-dialog>
    <!--抽屉-->
    <el-drawer
        title="数据字典明细列表"
        :visible.sync="drawer"
        :direction="direction">
      &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
      &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
      &emsp;&emsp;&emsp;&emsp;&emsp;&emsp;
      &emsp;&emsp;
      <el-button type="primary" @click="dailAdd">新增</el-button>
      <!--数据字典明细列表-->
      <el-table :data="sdtDetails" highlight-current-row v-loading="dailListLoading" style="width: 100%;">
        <el-table-column type="index" label="编号" width="80">
        </el-table-column>
        <el-table-column prop="name" label="数据字典名" width="150" sortable>
        </el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <el-button size="small" @click="DailEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="DailDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页查询工具条-->
      <el-pagination
          @size-change="sizeChange"
          @current-change="currentChange"
          :current-page="sddPage.currentPage"
          :page-sizes="sddPage.pageSizes"
          :page-size="sddPage.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="sddPage.total"
          style="float:right;">
      </el-pagination>
    </el-drawer>
  </section>
</template>

<script>
export default {
  data() {
    return {
      //高级查询参数
      filters: {
        keyword: ''
      },
      sdtPage:{
        total: 0,
        currentPage: 1,
        pageSize:5,
        pageSizes:[5,10,15,30],
      },
      sddPage:{
        total: 0,
        currentPage: 1,
        pageSize:5,
        pageSizes:[5,10,15,30],
      },
      sdts: [],//数据字典列表
      sdtDetails:[],//数据字典明细列表
      listLoading: false,//数据字典列表忙等
      dailListLoading:false,//数据字典明细列表忙等
      sels: [],//批量删除id数据
      title:'',//数据字典保存界面标题
      detailTitle:'',//字典明细保存界面标题
      drawer:false,//抽屉是否打开
      direction: 'rtl',//抽屉弹出方向
      types_id:'',
      state:{
      },
      saveDetailFormVisible: false,//字典明细新增界面是否显示
      saveFormVisible: false,//数据字典新增界面是否显示
      saveLoading: false,
      saveDetailLoading:false,
      saveFormRules: {
        name: [
          { required: true, message: '字典名称不能为空', trigger: 'blur' }
        ],
        sn: [
          { required: true, message: '字典编号不能为空', trigger: 'blur' }
        ],
      },
      saveDetailFormRules: {
        name: [
          { required: true, message: '明细名不能为空', trigger: 'blur' }
        ]
      },
      //数据字典新增界面数据
      saveForm: {
        id: null,
        sn:"",
        name: '',
      },
      //数据字典新增界面数据
      saveDetailForm:{
        id: null,
        name: '',
        types_id:this.types_id
      }
    }
  },
  methods: {
    //数据字典当前页变化
    handleCurrentChange(val) {
      this.sdtPage.currentPage = val;
      this.getSdts();
    },
    //数据字典当前页条数
    handleSizeChange(val){
      this.sdtPage.pageSize = val
      this.getSdts();
    },
    //绑定行双击事件
    sdtDailTable(val){
      console.log(val.id)
      this.types_id = val.id
      this.drawer = true
      this.getSdtDails()
    },
    //高级查询
    getQuerySdts(){
      this.sdtPage.currentPage = 1
      this.getSdts()
    },
    //获取字典列表
    getSdts() {
      let para = {
        currentPage: this.sdtPage.currentPage,
        pageSize:this.sdtPage.pageSize,
        keyword:this.filters.keyword,
      };
      this.listLoading = true;
      this.$http.post("/sdt",para).then(result=>{
        console.log(result)
        this.sdts = result.data.data
        this.sdtPage.total = result.data.total
        this.listLoading = false;
      })
    },

    //字典列表删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/sdt/"+row.id).then(result=>{
          console.log(result)
          if (result.data.success){
            this.$message({
              message: result.data.msg,
              type: 'success'
            });
            this.sdtPage.currentPage = 1;
            this.getSdts()
          }else {
            this.$message({
              message: result.data.msg,
              type: 'error'
            });
          }
          this.listLoading = false;
        }).catch(result=>{
          this.listLoading = false;
        })
      }).catch(() => {
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      if(this.$refs['saveForm']){
        this.$refs['saveForm'].resetFields();//重置表单验证
      }
      this.title = '编辑'
      this.saveFormVisible = true;
      this.saveForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      if(this.$refs['saveForm']){
        this.$refs['saveForm'].resetFields();//重置表单验证
      }
      this.title = "新增"
      this.saveFormVisible = true;
      this.saveForm = {
        id: null,
        sn:"",
        name: '',
      };
    },
    //保存
    addSubmit: function () {
      //手动触发验证规则
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //开启忙等
            this.saveLoading = true;
            //NProgress.start();
            //克隆表单数据
            let para = Object.assign({}, this.saveForm);
            console.log(para)
            this.$http.put("/sdt",para).then(result=>{
              //判断是否成功
              if (result.data.success){
                //关闭忙等
                this.saveLoading = false;
                this.$message({
                  message: result.data.msg,
                  type: 'success'
                });
                //重置表单数据
                this.$refs['saveForm'].resetFields();
                //关闭模态框
                this.saveFormVisible = false;
                //刷新列表
                this.getSdts();
              }else {
                this.$message({
                  message: result.data.msg,
                  type: 'warning'
                });
                this.saveLoading = false
              }
            })
          });
        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    /*    batchRemove: function () {
          var ids = this.sels.map(item => item.id);
          this.$confirm('确认删除选中记录吗？', '提示', {
            type: 'warning'
          }).then(() => {
            this.listLoading = true; //开启忙等
            this.$http.patch("/sdt/del",ids).then(result=>{
              this.listLoading = false; //关闭忙等
              //判断是否成功
              if (result.data.success){
                this.$message({
                  message: result.data.msg,
                  type: 'success'
                });
                //刷新列表
                this.sdtPage.currentPage = 1;
                this.getSdts();
              }else {
                this.$message({
                  message: result.data.msg,
                  type: 'warning'
                });
              }
            })
          }).catch(() => {

          });
        },*/
    //-------------------------------字典明细方法---------------------------------------------------
    //字典明细当前页变化
    currentChange(val) {
      this.sddPage.currentPage = val;
      this.getSdtDails();
    },
    //字典明细当前页条数
    sizeChange(val){
      this.sddPage.pageSize = val
      this.getSdtDails();
    },
    //获取字典明细列表
    getSdtDails(){
      let para ={
        currentPage: this.sddPage.currentPage,
        pageSize:this.sddPage.pageSize,
        types_id:this.types_id
      }
      this.dailListLoading = true;
      this.$http.post("/sdd",para).then(result=>{
        this.sdtDetails = result.data.data
        this.sddPage.total = result.data.total
        this.dailListLoading = false;
      })
    },
    //字典明细删除
    DailDel(index, row){
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.dailListLoading = true;
        this.$http.delete("/sdd/"+row.id).then(result=>{
          console.log(result)
          if (result.data.success){
            this.$message({
              message: result.data.msg,
              type: 'success'
            });
            this.sddPage.currentPage = 1;
            this.getSdtDails()
          }else {
            this.$message({
              message: result.data.msg,
              type: 'error'
            });
          }
          this.dailListLoading = false;
        }).catch(result=>{
          this.dailListLoading = false;
        })
      }).catch(() => {
      });
    },
    //字典明细编辑界面
    DailEdit(index, row){
      if(this.$refs['saveDetailForm']){
        this.$refs['saveDetailForm'].resetFields();//重置表单验证
      }
      this.detailTitle = '编辑'
      this.saveDetailFormVisible = true
      this.saveDetailForm = Object.assign({}, row);
    },
    //字典明细添加界面
    dailAdd(index, row){
      if(this.$refs['saveDetailForm']){
        this.$refs['saveDetailForm'].resetFields();//重置表单验证
      }
      this.detailTitle = '新增'
      this.saveDetailFormVisible = true
      this.saveDetailForm = {
        id: null,
        name: '',
        types_id:this.types_id
      };
      // console.log(this.types_id)
    },
    addDetailSubmit(){
      //手动触发验证规则
      this.$refs.saveDetailForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //开启忙等
            this.saveDetailLoading = true;
            //克隆表单数据
            let para = Object.assign({}, this.saveDetailForm);
            console.log(para)
            this.$http.put("/sdd",para).then(result=>{
              //判断是否成功
              if (result.data.success){
                //关闭忙等
                this.saveDetailLoading = false;
                this.$message({
                  message: result.data.msg,
                  type: 'success'
                });
                //重置表单数据
                this.$refs['saveDetailForm'].resetFields();
                //关闭模态框
                this.saveDetailFormVisible = false;
                //刷新列表
                this.getSdtDails();
              }else {
                this.$message({
                  message: result.data.msg,
                  type: 'warning'
                });
                this.saveDetailLoading = false
              }
            })
          });
        }
      });
    }
  },
  mounted() {
    //加载数据列表
    this.getSdts();
  }
}

</script>

<style scoped>

</style>